package observer;

import java.util.ArrayList;
import java.util.List;

public class Subject {
    private List<Observer> observerList = new ArrayList<>();
    private int state;

    // 通知所有观察者
    private void notifyAllObservers(){
        for(Observer o:observerList){
            o.update();
        }
    }

    // 注册观察者
    public Boolean attach(Observer observer){
        this.observerList.add(observer);
        return true;
    }

    public List<Observer> getObserverList() {
        return observerList;
    }

    public void setObserverList(List<Observer> observerList) {
        this.observerList = observerList;
    }

    public int getState() {
        return state;
    }

    public void setState(int state) {
        this.state = state;
        notifyAllObservers();
    }

    @Override
    public String toString() {
        return "Subject{" +
                "observerList=" + observerList +
                ", state=" + state +
                '}';
    }
}
